<%@Page Language="VB"%>

<%@Import Namespace="System.Data" %>

<%@ Register TagPrefix="wrox" TagName="connect" Src="..\global\connect-strings.ascx" %>
<%@ Register TagPrefix="wrox" TagName="getdataview" Src="..\global\get-dataset-control.ascx" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head>
<title>Repeated-Value Data Binding to a DataSet Object</title>
<style type="text/css">
body, td {font-family:Tahoma,Arial,sans-serif; font-size:10pt}
input {font-family:Tahoma,Arial,sans-serif; font-size:9pt}
.heading {font-family:Tahoma,Arial,sans-serif; font-size:14pt; font-weight:bold}
.subhead {font-family:Tahoma,Arial,sans-serif; font-size:12pt; font-weight:bold; padding-bottom:5px}
.cite {font-family:Tahoma,Arial,sans-serif; font-size:8pt}
</style></head>
<body bgcolor="#ffffff">
<span class="heading">Repeated-Value Data Binding to a DataSet Object</span><hr />
<!--------------------------------------------------------------------------->

<%'-- insert connection string script --%>
<wrox:connect id="ctlConnectStrings" runat="server"/>

<%'-- insert the control that creates the DataSet --%>
<wrox:getdataview id="ctlDataView" runat="server"/>

HTML <b>&lt;select&gt;</b> elements:<br />
<select id="MySelectList" runat="server" /><p />

<b>&lt;ASP:DropDownList&gt;</b> controls:<br />
<ASP:DropDownList id="MyDropDown" runat="server" /><p />

<b>&lt;ASP:ListBox&gt;</b> controls:<br />
<ASP:ListBox id="MyASPList" runat="server" /><p />

<b>&lt;ASP:DataGrid&gt;</b> control:<br />
<ASP:DataGrid id="MyDataGrid" runat="server" /><p />

<b>&lt;ASP:Repeater&gt;</b> control:<br />
<ASP:Repeater id="MyRepeater" runat="server">
  <ItemTemplate>
    <div>
      <b><%# Container.DataItem("Title") %></b><br />
      ISBN: <%# Container.DataItem("ISBN") %> &nbsp;
      Published: <%# DataBinder.Eval(Container.DataItem, "PublicationDate", "{0:D}") %>
    </div>
  </ItemTemplate>
</ASP:Repeater><p />

<b>&lt;ASP:DataList&gt;</b> control:<br />
<ASP:DataList id="MyDataList" runat="server">
  <ItemTemplate>
      <b><%# Container.DataItem("Title") %></b><br />
      ISBN: <%# Container.DataItem("ISBN") %> &nbsp;
      Published: <%# DataBinder.Eval(Container.DataItem, "PublicationDate", "{0:D}") %>
  </ItemTemplate>
</ASP:DataList><p />

<b>&lt;ASP:CheckBoxList&gt;</b> control:<br />
<ASP:CheckBoxList id="MyCheckList" runat="server" /><p />

<b>&lt;ASP:RadioButtonList&gt;</b> control:<br />
<ASP:RadioButtonList id="MyRadioList" runat="server" /><p />

<!--------------------------------------------------------------------------->

<script language="vb" runat="server">

Sub Page_Load()

   'get connection string from ..\global\connect-strings.ascx user control
   Dim strConnect As String  = ctlConnectStrings.OLEDBConnectionString

   'specify ISBN code to select some rows from the database
   Dim strISBN As String = "18610029%"

   'create a variable to hold an instance of a DataView object
   Dim objDataSet As DataSet

   'get dataset from get-dataset-control.ascx user control
   objDataSet = ctlDataView.BooksDataSet(strConnect, strISBN)

   If IsNothing(objDataSet) Then Exit Sub

   'set the DataSource property of the controls

   '<select> list displays values from the Title column
   'and uses the ISBN as the <option> values
   'DataMember indicates which table to use from DataSet
   MySelectList.DataSource = objDataSet
   MySelectList.DataMember = "Books"
   MySelectList.DataValueField = "ISBN"
   MySelectList.DataTextField = "Title"

   'do same with ASP: dropdown list control
   MyDropDown.DataSource = objDataSet
   MyDropDown.DataMember = "Books"
   MyDropDown.DataValueField = "ISBN"
   MyDropDown.DataTextField = "Title"

   'in this case, set DataMember to Authors table
   'and use ISBN as value and display LastName
   MyASPList.DataSource = objDataSet
   MyASPList.DataMember = "Authors"
   MyASPList.DataValueField = "ISBN"
   MyASPList.DataTextField = "LastName"

   'a DataGrid can figure out the columns in the DataSet
   'by itself, so we just set the DataSource and DataMember
   MyDataGrid.DataSource = objDataSet
   MyDataGrid.DataMember = "Books"

   'the Repeater and DataList require <ItemTemplate> entries
   'that specify the columns - this is done within the element
   'definition earlier in the page
   MyRepeater.DataSource = objDataSet
   MyRepeater.DataMember = "Books"
   MyDataList.DataSource = objDataSet
   MyDataList.DataMember = "Books"

   'in the CheckboxList we'll display the Title and
   'use the Value as the control value
   MyCheckList.DataSource = objDataSet
   MyCheckList.DataMember = "Books"
   MyCheckList.DataValueField = "ISBN"
   MyCheckList.DataTextField = "Title"

   'in the RadioList we'll display and format the
   'Value and use the Key as the control value
   MyRadioList.DataSource = objDataSet
   MyRadioList.DataMember = "Books"
   MyRadioList.DataValueField = "ISBN"
   MyRadioList.DataTextField = "PublicationDate"
   MyRadioList.DataTextFormatString = "Published on {0:dddd, MMMM dd, yyyy}"

   'finally, bind all the controls on the page
   Page.DataBind()

End Sub

</script>

<!--------------------------------------------------------------------------->
<hr /><span class="cite">[<a href="../global/viewsource.aspx">view source</a>] &nbsp; &nbsp; &nbsp; &nbsp;
&copy;2001 <a class="cite" href="http://www.wrox.com/">Wrox Press</a> -
<a class="cite" href="http://www.wrox.com/Books/Book_Details.asp?isbn=1861004885">Professional ASP.NET</a> (ISBN: 1-861004-88-5)</span>
</body>
</html>
